package indi.kider.server.employee.api;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.Waiter;
import indi.kider.server.employee.service.WaiterService;
import indi.kider.server.employee.service.related.AuthcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by shixj on 2018/4/26.
 */
@RestController
@RequestMapping("waiter")
public class WaiterController {
    @Autowired
    private WaiterService waiterService;
    @Autowired
    private AuthcService authcService;
    /**
     * @api {GET} /{id}
     * @apiGroup waiter
     * @apiDescription 根据id获取服务人员信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/waiter/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"id":1,"enterpriseId":1,"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","workType":2,"remark":"2"}}
     * */
    @GetMapping("{id}")
    public CommonResponse<Waiter> getWaiterById(@PathVariable Long id) {
        return CommonResponse.buildGetSuccessResponse(waiterService.getWaiterById(id));
    }
    /**
     * @api {POST} /list
     * @apiGroup waiter
     * @apiDescription 根据字段查询获取服务人员列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/waiter/list
     * {"num":"0","size":"8","query":{"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","workType":2,"remark":"2"}}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"rows":[{"id":1,"enterpriseId":1,"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","workType":2,"remark":"2"}],"total":1,"size":0}}
     * */
    @PostMapping("/list")
    public CommonResponse getWaiterPages(@RequestBody PageRequest<Waiter> request) {
        Employee employee = authcService.getCurrentUser().getData();
        if(employee!=null&&!employee.getRoles().equals(EmployeeRoleCons.ADMIN)){//超级管理员
            request.getQuery().setEnterpriseId(employee.getEnterpriseId());
        }
        return CommonResponse.buildGetSuccessResponse(waiterService.getWaiterPages(request));
    }
    /**
     * @api {POST} /list/all
     * @apiGroup waiter
     * @apiDescription 根据字段查询获取服务人员列表（不分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/waiter/list/all
     * {"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","workType":2,"remark":"2"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":[{"id":1,"enterpriseId":1,"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","workType":2,"remark":"2"}]}
     * */
    @PostMapping("/list/all")
    public CommonResponse getWaiters(@RequestBody Waiter waiter) {
        Employee employee = authcService.getCurrentUser().getData();
        if(employee!=null&&!employee.getRoles().equals(EmployeeRoleCons.ADMIN)){//超级管理员
            waiter.setEnterpriseId(employee.getEnterpriseId());
        }
        return CommonResponse.buildGetSuccessResponse(waiterService.getWaiters(waiter));
    }
    /**
     * @api {POST} /
     * @apiGroup waiter
     * @apiDescription 新增服务人员
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/waiter
     * {"enterpriseId":1,"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","workType":2,"remark":"2"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"201","msg":"保存成功！","data":{"id":1,"enterpriseId":1,"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"2","workType":2,"remark":"2"}}
     */
    @PostMapping("")
    public CommonResponse add(@RequestBody Waiter waiter, HttpServletRequest request) {
        try {
            return CommonResponse.buildAddSuccessResponse(waiterService.add(waiter));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /**
     * @api {PUT} /
     * @apiGroup waiter
     * @apiDescription 修改服务人员
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/waiter
     * {"id":1,"enterpriseId":1,"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"23","workType":2,"remark":"2"}
     * @apiSuccessExample {json} 返回样例:
     * {"status":"202","msg":"修改成功！","data":{"id":1,"title":"","phone":"1231","name":"shiqin","sex":1,"age":30,"cardId":"23","workType":2,"remark":"2"}}
     */
    @PutMapping("")
    public CommonResponse modify(@RequestBody Waiter waiter) {
        try {
            return CommonResponse.buildUpdateSuccessResponse(waiterService.modify(waiter));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /**
     * @api {DELETE} /{id}
     * @apiGroup waiter
     * @apiDescription 删除服务人员
     * @apiParamExample 请求样例：
     * http://localhost:9002/human-resources/waiter/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"204","msg":"删除成功！","data":""}}
     */
    @DeleteMapping("/{id}")
    public CommonResponse delById(@PathVariable Long id, HttpServletRequest request) {
        waiterService.delById(id);
        return CommonResponse.buildDelSuccessResponse("");
    }
}
